namespace NICWebApi.Extensions.Http.Options;

/// <summary>
/// HTTP选项配置
/// </summary>
public class HttpOptions
{
    /// <summary>
    /// 是否启用HTTP请求记录
    /// </summary>
    public bool LogRequestResponse = true;

    /// <summary>
    /// 头部信息
    /// </summary>
    public Heards IpHeaders = new Heards();

    /// <summary>
    /// 是否启用请求信息记录
    /// </summary>
    public bool EnableRequestLogging { get; set; } = true;

    /// <summary>
    /// 是否启用响应信息记录
    /// </summary>
    public bool EnableResponseLogging { get; set; } = true;

    /// <summary>
    /// 是否记录请求体
    /// </summary>
    public bool LogRequestBody { get; set; } = false;

    /// <summary>
    /// 是否记录响应体
    /// </summary>
    public bool LogResponseBody { get; set; } = false;

    /// <summary>
    /// 请求体最大记录长度（字节）
    /// </summary>
    public int MaxRequestBodyLength { get; set; } = 4096;

    /// <summary>
    /// 响应体最大记录长度（字节）
    /// </summary>
    public int MaxResponseBodyLength { get; set; } = 4096;

    /// <summary>
    /// 排除的路径（不会被记录）
    /// </summary>
    public List<string> ExcludedPaths { get; set; } = new List<string>
    {
      "/swagger",
      "/health",
      "/metrics"
    };

    /// <summary>
    /// 是否记录执行时间
    /// </summary>
    public bool LogExecutionTime { get; set; } = true;

    /// <summary>
    /// 慢请求阈值（毫秒），超过此值将被标记为慢请求
    /// </summary>
    public int SlowRequestThreshold { get; set; } = 500;

    /// <summary>
    /// 记录慢请求的级别
    /// </summary>
    public string SlowRequestLogLevel { get; set; } = "Warning";

    /// <summary>
    /// 记录普通请求的级别
    /// </summary>
    public string NormalRequestLogLevel { get; set; } = "Information";

    /// <summary>
    /// 错误请求的级别
    /// </summary>
    public string ErrorRequestLogLevel { get; set; } = "Error";

    /// <summary>
    /// 记录请求头
    /// </summary>
    public bool LogHeaders { get; set; }

    /// <summary>
    /// 是否启用详细头信息记录
    /// </summary>
    public bool EnableDetailedHeaderLogging { get; set; } = false;

    /// <summary>
    /// 要记录的请求头（如果启用详细头信息记录）
    /// </summary>
    public List<string> HeadersToLog { get; set; } = new List<string>
    {
      "User-Agent",
      "Referer",
      "X-Forwarded-For",
      "X-Real-IP"
    };
}

/// <summary>
/// 请求头
/// </summary>
public class Heards
{
    /// <summary>
    /// 是否使用X-Real-IP标头
    /// </summary>
    public bool UseXRealIp { get; set; } = true;

    /// <summary>
    /// 是否使用X-Forwarded-For标头
    /// </summary>
    public bool UseXForwardedFor { get; set; } = true;

    /// <summary>
    /// 自定义IP标头
    /// </summary>
    public string CustomIpHeader { get; set; } = null;

    /// <summary>
    /// IP分隔符
    /// </summary>
    public string IpSeparator { get; set; } = ";";
}

/// <summary>
/// IP头信息配置选项
/// </summary>
public class IpHeaderOptions
{
    /// <summary>
    /// 是否启用X-Forwarded-For头获取客户端IP
    /// </summary>
    public bool UseXForwardedFor { get; set; } = true;

    /// <summary>
    /// 是否启用X-Real-IP头获取客户端IP
    /// </summary>
    public bool UseXRealIp { get; set; } = true;

    /// <summary>
    /// 自定义IP头名称
    /// </summary>
    public string? CustomIpHeader { get; set; }

    /// <summary>
    /// 请求头中的IP分隔符
    /// </summary>
    public string IpSeparator { get; set; } = ",";
}

/// <summary>
/// 用户代理解析选项
/// </summary>
public class UserAgentOptions
{
    /// <summary>
    /// 是否解析用户代理
    /// </summary>
    public bool ParseUserAgent { get; set; } = true;

    /// <summary>
    /// 是否缓存解析结果
    /// </summary>
    public bool CacheResults { get; set; } = true;

    /// <summary>
    /// 缓存有效期（分钟）
    /// </summary>
    public int CacheExpirationMinutes { get; set; } = 60;
}

/// <summary>
/// HTTP客户端配置选项
/// </summary>
public class HttpClientOptions
{
    /// <summary>
    /// 默认超时时间（秒）
    /// </summary>
    public int DefaultTimeoutSeconds { get; set; } = 30;

    /// <summary>
    /// 最大重试次数
    /// </summary>
    public int MaxRetryCount { get; set; } = 3;

    /// <summary>
    /// 是否自动跟随重定向
    /// </summary>
    public bool AllowAutoRedirect { get; set; } = true;

    /// <summary>
    /// 是否启用压缩
    /// </summary>
    public bool UseCompression { get; set; } = true;

    /// <summary>
    /// 是否使用缓存
    /// </summary>
    public bool UseCache { get; set; } = true;

    /// <summary>
    /// 客户端名称字典
    /// </summary>
    public Dictionary<string, string> ClientNames { get; set; } = new Dictionary<string, string>();

    /// <summary>
    /// 默认请求头字典
    /// </summary>
    public Dictionary<string, string> DefaultHeaders { get; set; } = new Dictionary<string, string>();
}